package com.zjdiepu.www.manage.api.dz;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.SortFieldType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.AbstractYwBusinessLoanVoNewMapper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessCarVoMapper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.dz.IDzMortgageService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessCarService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dz.dto.DzMortgage;
import com.zjdiepu.www.model.dz.ext.DzMortgageExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.util.YwExtHelper;

/**
 * 车辆抵押确认Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月11日 16时17分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DzMortgageController", produces="application/json;charset=UTF-8")
@Api(tags = {"车辆抵押确认"}, produces="application/json;charset=UTF-8")
public class DzMortgageController extends BaseController {
    @Resource
    private IDzMortgageService dzMortgageService;
    @Resource
    private IYwBusinessCarService ywBusinessCarService;
    @Resource
    private IYwBusinessLoanService ywBusinessLoanService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    @Resource
    private YwBusinessCarVoMapper ywBusinessCarVoMapper;
    @Resource
    private AbstractYwBusinessLoanVoNewMapper ywBusinessLoanVoMapper;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 抵押确认 <br/>
     * @param mortgageRegistration
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "抵押确认", response = Boolean.class, httpMethod="POST", tags={"抵押确认"})
    @RequestMapping(value = "mortgageRegistration", method = RequestMethod.POST)
    public ApiResponse mortgageRegistration(@Validated(Add.class) @ModelAttribute DzMortgage dzMortgage) {
    	AbstractSysUser currentUser = getCurrentUser();
        return dzMortgageService.saveMortgageRegistration(dzMortgage, currentUser);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: mortgageRegistrationList <br/>
     * @Description: 车牌登记列表 <br/>
     * @param ywBusinessQueryRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "抵押确认列表",response = YwBusiness.class, httpMethod="POST", tags={"抵押确认"})
    @RequestMapping(value = "mortgageRegistrationList", method = RequestMethod.POST)
    public ApiResponse mortgageRegistrationList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        YwExtHelper.initExt(ywBusinessQueryRequest, SortFieldType.LOAN_TIME);
//        ywBusinessQueryRequest.addBusiStatus(BusiStatus.CHE_PAI_YI_DENG_JI.getCode());
//        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
//        ywBusinessQueryRequest.setOrgId(currentUser.getOrgId());
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增车辆抵押确认 <br/>
     * @param dzMortgage
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增车辆抵押确认", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DzMortgage dzMortgage) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzMortgage.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzMortgage.setModifierName(currentUser.getName());
        dzMortgageService.saveUpdateAndDelete(dzMortgage, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除车辆抵押确认<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除车辆抵押确认", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzMortgageService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用车辆抵押确认<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用车辆抵押确认", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzMortgageService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用车辆抵押确认<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用车辆抵押确认", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzMortgageService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改车辆抵押确认 <br/>
     * @param dzMortgage
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改车辆抵押确认", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DzMortgage dzMortgage) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzMortgage.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzMortgage.setModifierName(currentUser.getName());
    	dzMortgageService.saveUpdateAndDelete(dzMortgage, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询车辆抵押确认<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询车辆抵押确认", response = DzMortgage.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DzMortgage dzMortgage = dzMortgageService.selectById(id);
       if(null == dzMortgage) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dzMortgage);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询车辆抵押确认 <br/>
     * @param dzMortgageExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询车辆抵押确认",response = DzMortgage.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DzMortgageExt dzMortgageExt){
        dzMortgageExt.notDeleted();
        PageVo page = getPage(dzMortgageExt);
        int records = dzMortgageService.countByExt(dzMortgageExt);
        page.setRecords(records);
        if (records > 0) {
            List<DzMortgage> dzMortgages = dzMortgageService.selectByExt(dzMortgageExt);
            page.setRows(dzMortgages);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
